////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.wordpress.util
{
	/**
	 * @private
	 * */ 
	public class WPDateUtil
	{
		private static const REG_MONTHS : String = '(January|February|March|April|May|June|July|August|September|October|November|December)';
		private static const REG_DAYS : String = '([0-9]+)'
		private static const REG_DAYS_SUFFIX : String = '(st|nd|rd|th)';
		private static const REG_YEAR : String = '([0-9]+)';
		private static const REG_HOUR : String = '([0-9]+)';
		private static const REG_MINUTES : String = '([0-9]+)';
		private static const REG_AM_PM : String = '(am|pm)';
		
		public static function dateFromString(str : String, withTime : Boolean) : Date {
			var pattern : String;
			var date : Date = new Date();
			var list : Array
			if (!withTime) {
				pattern = REG_MONTHS + ' ' + REG_DAYS + REG_DAYS_SUFFIX + ', ' + REG_YEAR;
				list = str.match(pattern);
				date.month = __getMonthIndexFromName(list[1]);
				date.date = list[2];
				date.fullYear = list[4];
				date.hours = 0;
				date.minutes = 0;
			} else {
				pattern = REG_MONTHS + ' ' + REG_DAYS + ', ' + REG_YEAR + ' at '+REG_HOUR+':'+REG_MINUTES + ' '+REG_AM_PM;
				list = str.match(pattern);
				date.month = __getMonthIndexFromName(list[1]);
				date.date = list[2];
				date.fullYear = list[3];
				date.hours = list[4];
				date.minutes = list[5];
			}
			date.seconds = 0;
			date.milliseconds = 0;
			
			return date;
		}
		
		private static function __getMonthIndexFromName(name : String) : int {
			return REG_MONTHS.replace('(', '').replace(')', '').split('|').indexOf(name);
		}
	}
}